package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.Massif;
import com.lsyw.intergrate.entity.SysUserMassif;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.entity.qo.SysUserMassifQo;
import com.lsyw.intergrate.service.MassifService;
import com.lsyw.intergrate.service.SysUserMassifService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人员地块关联 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-28
 */
@Api(tags = "人员地块关联")
@RestController
@RequestMapping("/sys-user-massif")
public class SysUserMassifController {

    @Autowired
    private SysUserMassifService sysUserMassifService;

    @Autowired
    private MassifService massifService;

    /**
     * 通过用户id查询权限内的工地
     */
    @GetMapping("/get-by-user-id")
    @ApiOperation(value = "通过用户id查询权限内的工地", notes = "通过用户id查询权限内的工地", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, paramType = "query", dataType = "String")
    })
    public Result getByUserId(String userId) {
        final LambdaQueryWrapper<SysUserMassif> lambda = new QueryWrapper<SysUserMassif>().lambda();
        lambda.eq(SysUserMassif::getDelFlag, 0).eq(SysUserMassif::getSysUserId, userId);
        List<SysUserMassif> list = this.sysUserMassifService.list(lambda);
        ArrayList<Massif> objects = new ArrayList<>();
        for (SysUserMassif sysUserMassif :list){
            String massifId = sysUserMassif.getMassifId();
            Massif massif = this.massifService.getById(massifId);
            objects.add(massif);
        }
        return Result.successResult(objects);
    }


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(sysUserMassifService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "先删除后修改", notes = "先删除后修改", httpMethod = "POST")
    public Result save(@RequestBody SysUserMassifQo sysUserMassifQo) {
        String userId = sysUserMassifQo.getUserId();
        List<String> massifIds = sysUserMassifQo.getMassifId();
        //先删除后修改
        final LambdaQueryWrapper<SysUserMassif> lambda = new QueryWrapper<SysUserMassif>().lambda();
        lambda.eq(SysUserMassif::getDelFlag, 0).eq(SysUserMassif::getSysUserId, userId);
        List<SysUserMassif> list = this.sysUserMassifService.list(lambda);
        for (SysUserMassif sysUserMassif :list){
            sysUserMassif.setDelFlag(1);
            this.sysUserMassifService.updateById(sysUserMassif);
        }


        for (String massifId : massifIds) {
            SysUserMassif sysUserMassif = new SysUserMassif();
            sysUserMassif.setSysUserId(userId);
            sysUserMassif.setMassifId(massifId);
            //   sysUserMassif.setGmtCreateUser();
            sysUserMassif.setGmtCreate(new Date());
            //   sysUserMassif.setGmtModifiedUser();
            sysUserMassif.setGmtModified(new Date());
            sysUserMassif.setDelFlag(0);
            this.sysUserMassifService.save(sysUserMassif);
        }
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            sysUserMassifService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody SysUserMassif sysUserMassif) {
        sysUserMassifService.updateById(sysUserMassif);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody SysUserMassifReqVo sysUserMassif) {
        final LambdaQueryWrapper<SysUserMassif> lambda = new QueryWrapper<SysUserMassif>().lambda();
        this.buildCondition(lambda, sysUserMassif);
        return Result.successResult(sysUserMassifService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<SysUserMassif> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<SysUserMassif> lambda = new QueryWrapper<SysUserMassif>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.sysUserMassifService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<SysUserMassif> lambda, SysUserMassifReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(SysUserMassif::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getSysUserId())) {
            lambda.eq(SysUserMassif::getSysUserId, param.getSysUserId());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(SysUserMassif::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(SysUserMassif::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(SysUserMassif::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(SysUserMassif::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(SysUserMassif::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(SysUserMassif::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(SysUserMassif::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(SysUserMassif::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, SysUserMassif::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class SysUserMassifReqVo extends SysUserMassif {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
